package cn.fraudmetrix.octopus.modules.mohe_test_case.controller;

import cn.fraudmetrix.octopus.modules.mohe_test_case.dao.SqlUtilDao;
import cn.fraudmetrix.octopus.modules.mohe_test_case.dao.TestCaseDao;
import cn.fraudmetrix.octopus.modules.mohe_test_case.entity.CaseActionEntity;
import cn.fraudmetrix.octopus.modules.mohe_test_case.entity.TestCaseEntity;
import cn.fraudmetrix.octopus.modules.mohe_test_case.utils.CreateCaseId;
import cn.fraudmetrix.octopus.modules.mohe_test_case.utils.HttpClientUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/case")
@Api(tags = "测试用例相关接口")
public class TestCaseController {
    @Resource
    private TestCaseDao testCaseDao;
    @Resource
    private SqlUtilDao sqlUtilDao;
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());
    CreateCaseId createCaseId = new CreateCaseId();
    @PostMapping(value = "/query")
    @ApiOperation(value = "查询所有用例")
    public List<TestCaseEntity> queryAll(){
        int i = 55;
        String sql = "select * from user where id="+i;
        System.out.println(sqlUtilDao.select(sql));
        return testCaseDao.queryAll();
    }
    //新增用例
    @PostMapping(value="/addCase")
    @ApiOperation(value = "新增用例")
    public boolean addCase(@RequestBody TestCaseEntity testCaseEntity){
        testCaseEntity.setCaseId(createCaseId.getCreateCaseId(
                testCaseEntity.getApplication(),
                testCaseDao.count(testCaseEntity)
                ));
        return testCaseDao.addCase(testCaseEntity);
    }
    //更新用例
    @PostMapping(value="/updateCase")
    @ApiOperation(value = "更新用例")
    public boolean updateCase(@RequestBody TestCaseEntity testCaseEntity){
        return testCaseDao.updateByCaseId(testCaseEntity);
    }
    //删除用例
    @PostMapping(value="/deleteByCaseId")
    @ApiOperation(value = "删除用例")
    public boolean deleteByCaseId(@RequestBody TestCaseEntity testCaseEntity){
        return testCaseDao.deleteByCaseId(testCaseEntity);
    }
    //按照用例编号查询
    @PostMapping(value ="/queryByCaseId")
    @ApiOperation(value = "按照用例编号查询")
    public List<TestCaseEntity> queryByCaseId(@RequestBody TestCaseEntity testCaseEntity){

        return testCaseDao.queryByCaseId(testCaseEntity);}
    //统计测试用例数量
    @PostMapping(value = "/count")
    @ApiOperation(value = "统计测试用例数量")
    public int count(@RequestBody TestCaseEntity testCaseEntity){
        return testCaseDao.count(testCaseEntity);
    }
    //按照创建者搜索
    @PostMapping(value = "/queryByDetail")
    @ApiOperation(value = "按照创建者搜索")
    public List<TestCaseEntity> queryByAuthor(@RequestBody TestCaseEntity testCaseEntity){
        return testCaseDao.queryByDetail(testCaseEntity);
    }

    @PostMapping(value ="/action")
    @ApiOperation(value = "单个调试")
    public CaseActionEntity action(@RequestBody TestCaseEntity testCaseEntity){
        CaseActionEntity caseActionEntity = new CaseActionEntity();
        List<TestCaseEntity> testCases = this.queryByCaseId(testCaseEntity);
        TestCaseEntity testCase = testCases.get(0);
        String url = testCase.getApi();
        String expectResult = testCase.getExpectResult();
        Map<String,Object> body = new HashMap<>();
        JSONObject jsonParam = JSONObject.parseObject(testCase.getParam());
        for(Map.Entry<String,Object> entry : jsonParam.entrySet()){
                body.put(entry.getKey(),entry.getValue());
                LOGGER.info("入参键值："+entry.getKey()+"===="+entry.getValue());
        }
        String result = HttpClientUtils.httpPostRequest(url,body);
        LOGGER.info("实际结果："+result);
        caseActionEntity.setActualResult(result);
        caseActionEntity.setExpectResult(expectResult);
        return caseActionEntity;
    }

}
